package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.ab.http.OnMessageResponse;
import com.ab.util.ListUtils;
import com.ab.util.ToastUtil;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.adapter.SolveProblemAdapter;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.QuizList;
import com.baidumap.administrator.happystudy.view.SwipyRefreshLayout;
import com.socks.library.KLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by C on 2017/3/25.
 * 老师解题
 */
public class SolveProblemActivity extends AppBaseActivity implements View.OnClickListener, OnMessageResponse {


    private ImageView mIvEdit;

    @Bind(R.id.listview)
    ListView mListView;
    @Bind(R.id.main_SwipyRefreshLayout)
    SwipyRefreshLayout mSwipyRefreshLayout;

    private static int currentPage = 1;

    private List<QuizList.DataBean> mdataBeenList = new ArrayList<>();
    private SolveProblemAdapter solveProblemAdapter;

    @Override
    public int bindLayout() {
        return R.layout.activity_solve_problem;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        solveProblemAdapter = new SolveProblemAdapter(this, mdataBeenList);
        mListView.setAdapter(solveProblemAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (mdataBeenList.get(position).getUid().equals(sHelper.getString(Constant.UID))) {
                    Bundle bundle = new Bundle();

                    bundle.putString("timu_id", mdataBeenList.get(position).getId());

                    startBundleActivity(bundle, SubjectDetailActivity.class);
                } else {
                    ToastUtil.showMessage(SolveProblemActivity.this, "本题目非你的提问，不能查看");
                }
            }
        });
//        mIvEdit = (ImageView) view.findViewById(R.id.iv_edit);
//        mIvEdit.setOnClickListener(this);

        // 设置分页第一页的索引 默认为0
        mSwipyRefreshLayout.setFirstIndex(currentPage);
//        mSwipyRefreshLayout.setDirection(SwipyRefreshLayoutDirection.TOP);
        // 监听事件
        mSwipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(int index) {
                currentPage = 1;

                mdataBeenList.clear();
                Api.quizLists(SolveProblemActivity.this, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID));

            }

            @Override
            public void onLoad(int index) {
                mSwipyRefreshLayout.setRefreshing(false);
            }
        });
        // LoadImage初始化
        currentPage = 1;

    }

    private void initTitleRightLayout() {
        mAbTitleBar.clearRightView();
        View rightViewMore = mInflater.inflate(R.layout.timu_edit, null);
        mAbTitleBar.addRightView(rightViewMore);
        ImageView imgAdd = (ImageView) rightViewMore.findViewById(R.id.iv_edit);

        imgAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                if (AbStrUtil.isEmpty(sHelper.getString(Constant.STU_UID))) {
//
//                    startActivity(CurriculumScheduleLoginActivity.class);
//                    return;
//                }
                startActivity(NewQuestionActivity.class);
            }
        });


    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        initTitleRightLayout();
        setHeaderTitle(R.string.timu);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mdataBeenList.clear();
        solveProblemAdapter.notifyDataSetChanged();
        Api.quizLists(this, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.iv_edit://编辑
//                Intent intent = new Intent(SolveProblemActivity.this, NewQuestionActivity.class);
//                startActivity(intent);
//                break;
        }
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {
        if (url.equals(Constant.QUIZ_LISTS) && jo != null) {
            QuizList quizList = new QuizList(jo.toString());

            if (quizList != null) {

                if (quizList.getCode().equals("200")) {

                    List<QuizList.DataBean> dataBeanList = quizList.getData();
                    for (int i = 0; i < dataBeanList.size(); i++) {
                        mdataBeenList.add(dataBeanList.get(i));
                    }

                    ListUtils.removeDuplicate(mdataBeenList);

                    currentPageAdd(mdataBeenList);

                    solveProblemAdapter.notifyDataSetChanged();

                } else {
                    ToastUtil.showMessage(this, quizList.getMsg());
                }
            }
            mSwipyRefreshLayout.setRefreshing(false);
        }
    }

    protected void currentPageAdd(List<QuizList.DataBean> mListView) {
        KLog.e("========" + currentPage);
        if (mListView.size() % 20 == 0 && mListView.size() > 0) {
            currentPage++;
        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }
}
